package cn.itbeien.manager.controller;

import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author beien
 * @date 2024-03-10 18:08
 * Copyright© 2024 beien
 */
//@RestController
//@RequestMapping("unionPay")
@Controller
public class UnionPayController {
   // @RequestMapping("{pages}/{callPay}")
    public String callPay(@PathVariable String pages,@PathVariable String callPay){
        return pages+"/"+callPay;
    }

    @RequestMapping("doPay")
    public String callPay(){
        return "/callPay";
    }

    @RequestMapping("unionPay")
    public String unionPay(){
        return "/unionPay";
    }

    //@RequestMapping("doPay")
    public String doPay(@RequestParam("signature") String signature,@RequestParam("timestamp") String timestamp,@RequestParam("nonce")String nonce,@RequestParam("echostr")String echostr){
        return echostr;
    }

    /**
     * 阿里回调该方法，推送auth_code
     * 支付宝开放平台回调方法
     * 该地址必须在开发平台授权 支付宝开放平台中为授权回调地址
     * @param auth_code
     * @param app_id
     * @param scope
     * @return
     */
    @RequestMapping("alipay")
    @ResponseBody
    public String aliPay(@RequestParam("auth_code") String auth_code, @RequestParam("app_id") String app_id,@RequestParam("scope")String scope){
        System.out.println("支付宝返回信息....................................................");
        getAlipayInfo(auth_code);
        System.out.println(auth_code);

        return "/alipay";
    }

    /**
     * 换取 access_token 和 user_id
     * @param authCode
     */
    public void getAlipayInfo(String authCode){
       try{
           // 初始化SDK

           AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig());


           // 构造请求参数以调用接口

           AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();



           // 设置刷新令牌

           //request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");



           // 设置授权码

           request.setCode(authCode);



           // 设置授权方式

           request.setGrantType("authorization_code");



           AlipaySystemOauthTokenResponse response = alipayClient.execute(request);

           System.out.println(response.getBody());


           if (response.isSuccess()) {

               System.out.println("调用成功");

           } else {

               System.out.println("调用失败");

               // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接

               // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);

               // System.out.println(diagnosisUrl);

           }
        }catch(Exception e){
           e.printStackTrace();
       }
    }

    /**
     * 设置alipay相关参数，用于发起获取userid请求
     * @return
     */
    private static AlipayConfig getAlipayConfig() {

        String privateKey  = "";

        String alipayPublicKey = "";

        AlipayConfig alipayConfig = new AlipayConfig();

        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");//获取用户userid网关地址

        alipayConfig.setAppId("");//支付宝开放平台生活号app appid

        alipayConfig.setPrivateKey(privateKey);//设置私钥

        alipayConfig.setFormat("json");

        alipayConfig.setAlipayPublicKey(alipayPublicKey);//设置公钥

        alipayConfig.setCharset("UTF-8");

        alipayConfig.setSignType("RSA2");//签名算法类型

        return alipayConfig;

    }

    /**
     * 第一步：唤起半屏授权框,用户授权通过alipay调用在开放平台配置的地址
     * @param data
     * @return
     */
    @RequestMapping("authRedirect")
    public String authRedirect(String data){
        System.out.println();
        return "/success";
    }

}
